package com.bazaaroid.client.web.gwt.partnermodule.client;

import com.bazaaroid.client.web.gwt.partnermodule.client.presenter.PartnerModulePresenter;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.PartnerModuleViewImpl;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.PartnerModuleView.Presenter;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements Presenter, ValueChangeHandler<String> {
	private final HandlerManager eventBus;
	private HasWidgets container;
	private PartnerModuleViewImpl partnerModuleView = null;

	public AppController(HandlerManager eventBus) {
		this.eventBus = eventBus;
		bind();
	}

	private void bind() {
		History.addValueChangeHandler(this);
	}

	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("list");
		} else {
			History.fireCurrentHistoryState();
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		if (partnerModuleView == null) {
			partnerModuleView = new PartnerModuleViewImpl();
		}
		new PartnerModulePresenter(eventBus, partnerModuleView).go(container);
	}

}
